<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../statics/layui/css/layui.css">
    <style>
        .title {
            text-align: center;
        }
    </style>
</head>
<body>
<br>
<div class="title">
    <h1>电子图书信息列表</h1>

</div>
<br>
<br>
<h3>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">图书分类</label>
                <div class="layui-input-inline">
                    <select id="category" name="modules" lay-verify="required" lay-search="">

                    </select>
                </div>
                <button class="layui-btn" id="select">查询</button>
            </div>
        </div>
    </form>
</h3>
<h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button class="layui-btn layui-btn-normal" id="add">新增电子图书</button></h3>
<table class="layui-hide" id="test1" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script type="text/javascript" src="../statics/layui/layui.all.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','jquery'], function(table, form, $){
        var $ = layui.jquery, table = layui.table, form = layui.form;
        table.render({
            elem: '#test1'
            ,url:'listbook.action'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'图书编号', width:120, fixed: 'left', unresize: true, sort: true}
                ,{field:'categoryName', title:'图书分类', width:220, edit: 'text',sort: true}
                ,{field:'title', title:'图书名称', width:220, edit: 'text',sort: true}
                ,{field:'summary', title:'图书摘要', width:380, edit: 'text', sort: true}
                ,{field:'uploaduser', title:'上传人', width:100,sort: true}
                ,{field:'createdate', title:'上传时间',width:300,sort: true,templet: "<div>{{layui.util.toDateString(d.createdate, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                ,{field:'', title:'操作',toolbar: '#barDemo'}
            ]]
            ,page: true //开启分页
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                var arr = eval(res);
                let v = {};
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "getEntryCount.action",
                    data: {
                    },
                    async: false,
                    error: function (request) {
                    },
                    success: function (data) {
                        v.total = data;
                    }
                });
                if(res && res.length >=0){
                    v.status = 0
                    v.message = '成功'
                    v.list= arr
                }else{
                    v.total = 0
                    v.status = 1
                    v.message = '失败'
                    v.list= []
                }
                return {
                    "code": v.status, //解析接口状态
                    "msg": v.message, //解析提示文本
                    "count": v.total, //解析数据长度
                    "data": v.list //解析数据列表
                };
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });

        $(function(){
            $.ajax({
                cache: true,
                type: "POST",
                url: "getCategory.action",
                data: {
                },
                async: false,
                error: function (request) {
                },
                success: function (data) {
                    $("#category").empty();
                    var arr = $.parseJSON(data);
                    var str = "<option value=''>直接选择或搜索选择</option>";
                    for(var i = 0; i<arr.length;i++){
                        str += "<option value='"+arr[i].id+"'>"+arr[i].name+"</option>";
                    }
                    $("#category").append(str);
                    form.render();
                }
            });
            $("#select").click(function(){


            });
        })
    });
</script>
</body>
</html>